Module# 09: Table and Map                                           Lecture#38: Applications of Map Part-I

 

// Example 38.1: Creating a Map

 

/* This example creates a map container and add objects into it using the put() method. */

 

import java.util.*;

 

class HashMapCreateDemo {

      public static void main(String args[]) {

            // Create a hash map object as a container.

            HashMap<Double, String>hMap = new HashMap<Double, String>();

 

            // Put elements to the map container

            hMap.put(200.0, "OK");

            hMap.put(303.0, "See Other");

            hMap.put(404.0, "Not Found");

            hMap.put(500.0, "Internal Server Error");

 

            System.out.println(hMap);  // Printing the container

     }

}

 

 

// Example 38.2: Adding object with duplicate values

 

/* This example creates a map container using the put() method and with duplicate entries. */

import java.util.*;

 

class HashMapDuplicateDemo {

      public static void main(String args[]) {

      // Create a hash map object as a container.

      HashMap<Integer, String>hMap = new HashMap<Integer, String>();

 

      // Put elements to the map container with duplicates

      hMap.put(200, "OK");

      hMap.put(303, "See Other");

      hMap.put(404, "Not Found");

      hMap.put(500, "Internal Server Error");

      hMap.put(303, "Invalid entry");

      hMap.put(101, "See Other");

 

      System.out.println(hMap);  // Printing the container

    }

}

// Example 38.3: Copying a Map into other

 

/* This example creates a map container that copies elements from an existing map. */

 

import java.util.*;

class HashMapCopyDemo {

      public static void main(String args[]) {

      // Create a hash map object as a container.

      Map<String, Double> hMap1 = new HashMap<>();

      // Put elements to the map container 

      hMap1.put("John Doe", new Double(3434.34));

      hMap1.put("Tom Smith", new Double(123.22));

      hMap1.put("Jane Baker", new Double(1378.00));

      hMap1.put("Tod Hall", new Double(99.22));

      hMap1.put("Ralph Smith", new Double(-19.08));

      System.out.println(hMap1);  // Printing the container

      // Create a copy of a hMap1 to hMap2

      Map<String,Double> hMap2 = new HashMap<>(hMap1);

      // Add data into hMap2

      hMap1.put("Robin Keith", new Double(423.22));

      hMap2.put("Peter Hwang", new Double(178.00));

      System.out.println(hMap2);  // Printing the container

     }

}

 

 

// Example 38.4: Retrieving objects from a Map container

 

/* This example illustrates how an object stored in a map framework can be accessed using get(). */

 

import java.util.*;

class HashMapAcsessDemo {

      public static void main(String args[]) {

      // Create a hash map object as a container.

      Map<String, Double>hMap = new HashMap<>();

      // Put elements to the map container 

      hMap.put("John Doe", new Double(3434.34));

      hMap.put("Tom Smith", new Double(123.22));

      hMap.put("Jane Baker", new Double(1378.00));

      hMap.put("Tod Hall", new Double(99.22));

      hMap.put("Ralph Smith", new Double(-19.08));

      // Deposit 1000 into John Doe's account.

      double balance = hMap.get("John Doe");

      hMap.put("John Doe", balance + 1000);    

 

      System.out.println("John Doe's current balance: " + hMap.get("John Doe"));

     }

}

 

// Example 38.5: Removing objects from a Map container

 

/* There are two methods, namely remove() and replace()  that you can use for remove an entry in a map framework. The following example illustrates how an object stored in a map framework can be removed.  */

import java.util.*;

class HashMapRemoveDemo {

      public static void main(String args[]) {

      // Create a hash map object as a container.

      Map<String, Double>hMap = new HashMap<>();

      // Put elements to the map container 

      hMap.put("John Doe", new Double(3434.34));

      hMap.put("Tom Smith", new Double(123.22));

      hMap.put("Jane Baker", new Double(1378.00));

      hMap.put("Tod Hall", new Double(99.22));

      hMap.put("Ralph Smith", new Double(-19.08));

      Double val = hMap.remove("Jane Baker");

      if (val != null) {System.out.println("Removed value: " + val);}     

           System.out.println(hMap);

      hMap.remove("Tod Hall", 99.22);

      System.out.println(hMap);

      hMap.replace("Ralph Smith", 545.67);

      System.out.println(hMap);

     }

}

 

 

// Example 38.6: Retrieving objects from a Map container

 

/* The following example illustrates how an object stored in a map framework can be accessed. For this, you should use the get(). */

 

import java.util.*;

 

class HashMapAcsessDemo {

     public static void main(String args[]) {

      // Create a hash map object as a container.

      Map<String, Double>hMap = new HashMap<>();

 

      // Put elements to the map container 

      hMap.put("John Doe", new Double(3434.34));

      hMap.put("Tom Smith", new Double(123.22));

      hMap.put("Jane Baker", new Double(1378.00));

      hMap.put("Tod Hall", new Double(99.22));

      hMap.put("Ralph Smith", new Double(-19.08));

 

      // Deposit 1000 into John Doe's account.

      double balance = hMap.get("John Doe");

      hMap.put("John Doe", balance + 1000);    

 

      System.out.println("John Doe's current balance: " + hMap.get("John Doe"));

     }

}

 

// Example 38.7: Accessing the status of a map

 

/* There are two methods, namely size() and isEmpty()  that can be used to check the status of a map container.  The following example illustrates how an object stored in a map framework can be viewed. */

 

class HashMapViewDemo {

     public static void main(String args[]) {

      // Create a hash map object as a container.

      HashMap<Integer, String>hMap = new HashMap<Integer, String>();

      // Put elements to the map container

      hMap.put(200, "OK");

      hMap.put(303, "See Other");

      hMap.put(404, "Not Found");

      hMap.put(500, "Internal Server Error");

      // Checking the container

      if (hMap.isEmpty()) {

          System.out.println("Error: The container is empty");

      } else {

          System.out.println(hMap);  // Printing the container

      }

      // Printing the size of the container

      System.out.println("Size : " + hMap.size());

     }

}

 

 

// Example 38.8: Map iteration

 

/* This program illustrates the above here ways f iterating over a map container */

 

import java.util.*;

 

class HashMapIterationDemo {

     public static void main(String args[]) {

      // Create a hash map object as a container.

      Map<String, String>mapCountryCodes = new HashMap<>();

  

      mapCountryCodes.put("1", "USA");

      mapCountryCodes.put("44", "United Kingdom");

      mapCountryCodes.put("33", "France");

      mapCountryCodes.put("81", "Japan");

      mapCountryCodes.put("91", "India");

 

      // Collection view using keySet()

      Set<String>setCodes = mapCountryCodes.keySet();

      Iterator<String> iterator = setCodes.iterator();

      while(iterator.hasNext()) {

                String code = iterator.next();

                String country = mapCountryCodes.get(code);

                System.out.println(code + " => "+ country);

      }

           

      // Collection view using values()

      Collection<String> countries = mapCountryCodes.values();

      for(String country : countries) {

               System.out.println(country);

      }

 

      // Collection view using entrySet()

      Set<Map.Entry<String, String>> entries =

      mapCountryCodes.entrySet();

      for(Map.Entry<String, String>entry : entries) {

               String code = entry.getKey();

               String country = entry.getValue();

         System.out.println(code + " => "+ country);

          }

 

      // Collection view using Lambda expression

      mapCountryCodes.forEach((code, country) ->

      System.out.println(code + " => "+ country));

 

      }

}

 

// Example 38.9: Performing bulk operations with Maps

 

/* This program illustrates the above here ways f iterating over a map container */

 

import java.util.*;

class HashMapBulkOperationDemo {

     public static void main(String args[]) {

      // Create two map object containers.

      Map<Integer, String>countryCodesEU = new HashMap<>();

      countryCodesEU.put(44, "United Kingdom");

      countryCodesEU.put(33, "France");

      countryCodesEU.put(49, "Germany");

      Map<Integer, String>countryCodesWorld = new HashMap<>();

      countryCodesWorld.put(1, "United States");

      countryCodesWorld.put(86, "China");

      countryCodesWorld.put(82, "South Korea");

      System.out.println("Before: " + countryCodesWorld);

      // Merger two containers using putAll()

      countryCodesWorld.putAll(countryCodesEU);

      System.out.println("After: " + countryCodesWorld);

      // Clear one map container

      countryCodesEU.clear();

      System.out.println("Is map empty? "+ countryCodesEU.isEmpty());

     }

}